package main;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ICIDUtil {
	
	
private final static Logger log=LoggerFactory.getLogger(ICIDUtil.class);
	
	public static String toUID(String ICID){
		log.debug("icid:"+ICID);
		
		StringBuilder sb=new StringBuilder();
		for (int i = 12; i >=7 ; i--) {
			sb.append(hex2Graph(ICID.substring(i*2, i*2+2)));
		}
		
		sb.append(ICID.substring(12, 14));
		sb.append(ICID.substring(8, 10));
		sb.append(ICID.substring(4, 6));
		sb.append(ICID.substring(2, 4));
		sb.append(ICID.substring(0, 2));
		log.debug("uid:"+sb.toString());
		
		return sb.toString();
	}
	
	private static String hex2Graph(String hex){
		String result="";
		int oct=Integer.parseInt(hex, 16);
		if(oct>=48 && oct<=57){//0x30~0x39
			result+=oct-48;
		}else if(oct>=65 && oct<=70){//0x41~0x46
			result+=Integer.toHexString(oct-55).toUpperCase();
		}else{
			//TODO
		}
		return result;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String icid="868F10FFFF2E363935323636411F0101";
		String uid=toUID(icid);
		System.out.println(uid);

	}

}
